﻿
Namespace LightSwitchApplication

    Public Class ApplicationDataService


#Region "Query Methods"
        Private Sub Customers_All_PreprocessQuery(ByRef query As System.Linq.IQueryable(Of LightSwitchApplication.Customer))
            query = From c In query
                    Order By c.LastName, c.FirstName
        End Sub
#End Region
        
#Region "Save Pipeline"
        Private Sub Appointments_Inserting(entity As Appointment)
            entity.MsgID = Guid.NewGuid.ToString()
            entity.MsgSequence = 0
        End Sub

        Private Sub Appointments_Updating(entity As Appointment)
            'Update the sequence anytime the appointment is updated
            entity.MsgSequence += 1
        End Sub

        Private Sub Appointments_Inserted(entity As Appointment)
            'Send an email notification when an appointment is inserted into the system
            Dim isCanceled = False
            SMTPMailHelper.SendAppointment(entity.User.Email,
                                         entity.Customer.Email,
                                         entity.Subject,
                                         entity.Notes,
                                         entity.Location,
                                         entity.StartTime,
                                         entity.EndTime,
                                         entity.MsgID,
                                         entity.MsgSequence,
                                         isCanceled)
        End Sub

        Private Sub Appointments_Updated(ByVal entity As Appointment)
            'Send an email update when an appointment is updated. 
            ' This helper class will send an update to the original appointment
            ' because we are tracking the sequence and ID
            Dim isCanceled = False
            SMTPMailHelper.SendAppointment(entity.User.Email,
                                    entity.Customer.Email,
                                    entity.Subject,
                                    entity.Notes,
                                    entity.Location,
                                    entity.StartTime,
                                    entity.EndTime,
                                    entity.MsgID,
                                    entity.MsgSequence,
                                    isCanceled)

        End Sub

        Private Sub Appointments_Deleting(entity As Appointment)
            'Send an email cancellation when an appointment is deleted.
            ' This helper class will send a cancellation because we are tracking the sequence and ID.
            Dim isCanceled = True
            SMTPMailHelper.SendAppointment(entity.User.Email,
                                    entity.Customer.Email,
                                    entity.Subject,
                                    entity.Notes,
                                    entity.Location,
                                    entity.StartTime,
                                    entity.EndTime,
                                    entity.MsgID,
                                    entity.MsgSequence,
                                    isCanceled)

        End Sub
#End Region


        '#Region "Entity Access Control"
        '        'Only administrators can modify employee (HR) & material data
        '        Private Sub Employees_CanDelete(ByRef result As Boolean)
        '            result = Me.Application.User.HasPermission(Permissions.SecurityAdministration)
        '        End Sub

        '        Private Sub Employees_CanInsert(ByRef result As Boolean)
        '            result = Me.Application.User.HasPermission(Permissions.SecurityAdministration)
        '        End Sub

        '        Private Sub Employees_CanUpdate(ByRef result As Boolean)
        '            result = Me.Application.User.HasPermission(Permissions.SecurityAdministration)
        '        End Sub

        '        Private Sub Materials_CanDelete(ByRef result As Boolean)
        '            result = Me.Application.User.HasPermission(Permissions.SecurityAdministration)
        '        End Sub

        '        Private Sub Materials_CanInsert(ByRef result As Boolean)
        '            result = Me.Application.User.HasPermission(Permissions.SecurityAdministration)
        '        End Sub

        '        Private Sub Materials_CanUpdate(ByRef result As Boolean)
        '            result = Me.Application.User.HasPermission(Permissions.SecurityAdministration)
        '        End Sub
        '#End Region

        'Private Sub AllTransactionsByUser_PreprocessQuery(UserName As String, ByRef query As System.Linq.IQueryable(Of LightSwitchApplication.Appointment))
        '    query = LightSwitchFilter.Server.FilterExtensions.Filter(query, FilterTerm, Me.Application)
        'End Sub

     
    End Class

End Namespace
